//相关业务逻辑
$(function(){
//	scan.setConfig();
//	scan.getValue();
	page.bind();
	page.getOrderList();
});


var page={//页面总变量
		bind:function(){
			$("#orderId").bind("change",function(){
				var orderId=$(this).val();
				var orderIds=orderId.split(' ');
				orderId=orderIds[0];
			    $("#orderId").val(orderId);
				$.ajax({
					url:'order/getOrderDetail',
					data:{
						'orderId':orderId
					},
					success:function(data){
						if(data.result){
							data=data.data;
							if(data.length==1){
								$("#coustomId").val(data[0].customerId);
								$("#companyOrderId").val(data[0].companyOrderId);
								$("#factoryId").val(data[0].factoryId);
								$("#orderCount").val(data[0].orderCount);
								page.getColorCount(data[0].orderId);
							}
							if(data.length>1){
								var html=""
							    for(var i=0;i<data.length;i++){
							    	html+="<option value='"+data[i].orderId+"'>"+data[i].companyOrderId+" &nbsp;&nbsp;&nbsp;&nbsp;"+data[i].factoryId+" &nbsp;&nbsp;&nbsp;&nbsp;"+data[i].customerOrderId+"&nbsp;&nbsp;&nbsp;&nbsp;"+data[i].customerId+"</option>"
							    }
								$("#detailList").html(html);
							}
						}else{
							layer.msg(data.info,{icon:1});
							$("#orderCount").val("");
						}
					}
				});
			});
			
			
			
			
			
			$("#duanChuBtn").bind("click",function(){
				var orderId=$("#orderId").val();//订单编号
				var userName=$("#userName").text();//用后名
				if(orderId==''){
					layAlert("请先输入订单编号");
					return false;
				}
				var sendData=new Array();//传到后台数据
				 $(".count").each(function(){ 
					  var data=new Object();
			          data.duanChu = $(this).val();   
			          data.orderId=orderId;
			          data.rowIndex=$(this).attr("rowIn");
			          data.orderCount=$(this).attr("orderCount");
			          sendData.push(data);//添加到数组中  
			     });
				 
				//如果有需要设置短出的订单信息
				if(sendData.length>0){
					console.log(sendData);
					for(var i=0;i<sendData.length;i++){
						if(Number(sendData[i].duanChu)>Number(sendData[i].orderCount)){
							layAlert("短处数不能大于订单数");
							return false;
					}
				}
					ajaxUtil.ajaxRuquest('weiXin/setDuanChu',{'stringDate':JSON.stringify(sendData)},function(data){
						layer.msg(data, {icon: 1});
						var orderId=$("#orderId").val();
						page.getColorCount(orderId);
					});
				}
			});
		},
		getOrderList:function(){
			ajaxUtil.ajaxRuquestNoQuan('companyMaterial/getAllOrderSearchWhere', null, function (data) {
				var html = ""
				for (var i = 0; i < data.length; i++) {
					html += "<option value='" + data[i].orderId + " &nbsp;&nbsp;&nbsp;&nbsp;" + data[i].companyOrderId + " &nbsp;&nbsp;&nbsp;&nbsp;" + data[i].factoryId + " &nbsp;&nbsp;&nbsp;&nbsp;" + data[i].coustomOrderId + "&nbsp;&nbsp;&nbsp;&nbsp;" + data[i].coustomId + "' style='display:none'></option>"
				}
				$("#detailList").html(html);
			});
		},
		getColorCount:function(orderId){
			ajaxUtil.ajaxRuquest('weiXin/getColorCount',{'orderId':orderId},function(data){
				console.log(data);
				var html="";
				for(var i=0;i<data.length;i++){
				var className='one';
				if(data[i].rowIndex%2==0){
					className='two';
				}
				html+="<tr class='"+className+"'><td>"+data[i].machining+":<span class='color'>"+data[i].color+" "+data[i].colorDescription+"</span></td><td>订单数："+data[i].orderCount+"</td>"+
				   	 "</tr><tr class='"+className+"'><td>短处数量</td><td><input type='text' rowIn='"+data[i].rowIndex+"' orderCount='"+data[i].orderCount+"' class='input-text count "+data[i].rowIndex+"' value='"+data[i].duanChu+"'/></td></tr>";
			    }
				$("#kuangSe").html(html);
				if(data.length<=0){
					layer.msg('该订单没有需要设置短出数的明细', {icon: 1});
				}
				page.inputBind(data[data.length-1].rowIndex);
			});
		},
		idChange:function(){
			var orderId=$("#orderId").val();
			page.getOrderDetail(orderId);
			page.getColorCount(orderId);
		},
		inputBind:function(index){
			for(var i=1;i<=index;i++ ){
				$("."+i).bind("keyup",function(i){
					var re=/^[0-9]+[0-9]*]*$/;
					var str=$(this).val();
					if(!re.test(str)){
					   str=str.substring(0,str.length-1);
					}
					var className=$(this).attr("class");
					className=className.substring(className.length-1,className.length);
					$("."+className).val(str);
				});
			}
		}
}

